package cn.net.autocode.files.ocr;

import net.sourceforge.tess4j.Tesseract;

import java.util.Properties;

public class OcrFactory {

    private static Tesseract eng_tesseract;

    private static Tesseract chi_tesseract;

    private static Tesseract deu_tesseract;

    public static String datapath;

    private static String  defaultDatapath(){
        Properties properties = System.getProperties();
        String osName = properties.getProperty("os.name").toLowerCase();
        System.out.println(osName);
        if(osName.contains("window")){
            return "C:\\Program Files\\Tesseract-OCR\\tessdata";
        }else{
            return null;
        }
    }
    private static Tesseract init(String language) {
        Tesseract tesseract = new Tesseract();
        if(OcrFactory.datapath==null){
            datapath = defaultDatapath();
        }
        tesseract.setDatapath(datapath);
        tesseract.setLanguage(language);
        return tesseract;
    }

    public static Tesseract createTesseract(String language){
        Tesseract tesseract = switch(language){
            case "eng"->{
                if(eng_tesseract==null){
                    eng_tesseract = init(language);
                }
                yield eng_tesseract;
            }
            case "deu"->{
                if(deu_tesseract==null){
                    deu_tesseract = init(language);
                }
                yield deu_tesseract;
            }
            default->{
                if(chi_tesseract==null){
                    chi_tesseract = init("chi_sim");
                }
                yield chi_tesseract;
            }
        };
        return tesseract;
    }

    public static Tesseract createTesseract(){
        if(chi_tesseract==null){
            chi_tesseract = init("chi_sim");
        }
        return chi_tesseract;
    }
}
